package com.sgb.zhaocai.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.LabourWorkListEntity;
import com.sgb.zhaocai.entity.RequestBean;
import com.sgb.zhaocai.view.demand.fragment.LabourWorkFragment;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/7/1
 * AUTHOR:F_Q
 * Description:
 */
public class LabourWorkModel extends ViewModel {
    //列表
    public MutableLiveData<List<LabourWorkListEntity>> labourworklist = new MutableLiveData<>();
    //地址
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mAddressData = new MutableLiveData<>();
    //工程类型
    public MutableLiveData<List<CommonPopEntity>> ProjectTypeList = new MutableLiveData<>();
    //排序集合
    public MutableLiveData<List<CommonPopEntity>> SortTypeList = new MutableLiveData<>();
    //时间
    public MutableLiveData<List<CommonPopEntity>> startTimeList = new MutableLiveData<>();
    public boolean isLastPage;//是否可以加载更多
    /**
     * 列表
     * @param requestBean
     */
    public void LabourWorkList(LabourWorkFragment fragment,RequestBean requestBean) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getLabourWorkList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<LabourWorkListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<LabourWorkListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                labourworklist.setValue(data.getData().getList());
            }
        });
    }

    /**
     * 地址详情
     */
    public void getAddressModel(LabourWorkFragment fragment) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    mAddressData.setValue(data.getData());
                }
            }
        });
    }


    /**
     * 综合排序
     */
    public void getSortType() {
        List<CommonPopEntity> datas = new ArrayList<>();
        datas.add(new CommonPopEntity("1", "综合排序"));
        datas.add(new CommonPopEntity("3", "开工时间↓"));
        datas.add(new CommonPopEntity("2", "开工时间↑"));
        datas.add(new CommonPopEntity("4", "距离↓"));
        SortTypeList.setValue(datas);
    }

    /**
     * 开工时间
     * @param fragment
     */
    public void geStartTime(LabourWorkFragment fragment) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getfindProjectTimeSelect().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    startTimeList.setValue(data.getData());
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }

    /**
     * 工程类型
     * @param fragment
     */
    public void getProjectType(LabourWorkFragment fragment) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getFindEngineeringTypeSelect().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    ProjectTypeList.setValue(data.getData());
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }
}
